﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using YJKRevitParapet.Forms;

namespace YJKRevitParapet.Controller
{
	public class ExtensibleStorageManager
	{
		public static void WriteParapetExtensibleStorage(Wall parapet, ParapetForm.ParapetData data)
		{
			Guid guid = new Guid(ExtensibleStorageManager.m_GUID);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName("HY_Parapet_ESData");
				schemaBuilder.AddSimpleField("TypeName", typeof(string));
				schemaBuilder.AddSimpleField("Height", typeof(double)).SetUnitType(0);
				schemaBuilder.AddSimpleField("Thickness", typeof(double)).SetUnitType(0);
				schemaBuilder.AddSimpleField("HasCoping", typeof(bool));
				schemaBuilder.AddSimpleField("CopingStyleIndex", typeof(int));
				schemaBuilder.AddSimpleField("CopingStyleName", typeof(string));
				schema = schemaBuilder.Finish();
			}
			Entity entity = new Entity(schema);
			Field field = schema.GetField("TypeName");
			entity.Set<string>(field, data.TypeName);
			Field field2 = schema.GetField("Height");
            entity.Set<double>(field2, data.Height, (DisplayUnitType)6);
			Field field3 = schema.GetField("Thickness");
            entity.Set<double>(field3, data.Thickness, (DisplayUnitType)6);
			Field field4 = schema.GetField("HasCoping");
			entity.Set<bool>(field4, data.HasCoping);
			Field field5 = schema.GetField("CopingStyleIndex");
			entity.Set<int>(field5, data.CopingStyleIndex);
			Field field6 = schema.GetField("CopingStyleName");
			entity.Set<string>(field6, data.CopingStyleName);
			parapet.SetEntity(entity);
		}

		public static bool ReadParapetExtensibleStorage(Wall parapet, ref ParapetForm.ParapetData data)
		{
			Schema schema = Schema.Lookup(new Guid(ExtensibleStorageManager.m_GUID));
			if (schema == null)
			{
				return false;
			}
			Entity entity = parapet.GetEntity(schema);
			if (entity == null)
			{
				return false;
			}
			data.TypeName = entity.Get<string>(schema.GetField("TypeName"));
			data.Height = entity.Get<double>(schema.GetField("Height"), (Autodesk.Revit.DB.DisplayUnitType)6);
			data.Thickness = entity.Get<double>(schema.GetField("Thickness"), (Autodesk.Revit.DB.DisplayUnitType)6);
			data.HasCoping = entity.Get<bool>(schema.GetField("HasCoping"));
			data.CopingStyleIndex = entity.Get<int>(schema.GetField("CopingStyleIndex"));
			data.CopingStyleName = entity.Get<string>(schema.GetField("CopingStyleName"));
			return true;
		}

		public static bool HasParapetExtensibleStorage(Wall parapet)
		{
			Schema schema = Schema.Lookup(new Guid(ExtensibleStorageManager.m_GUID));
			if (schema == null)
			{
				return false;
			}
			Entity entity = parapet.GetEntity(schema);
			return entity != null && entity.Schema != null;
		}

		private static string m_GUID = "936E72D0-C195-42B6-8286-B22AC7B94324";
	}
}
